/**
 * Created by Eric.Zhang on 2017/4/7.
 */
var joyconn_layout_authorize = {};
joyconn_layout_authorize.getUrlParam =function(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
    var r = window.location.search.substr(1).match(reg);  //匹配目标参数
    if (r != null) return unescape(r[2]); return null; //返回参数值
}
joyconn_layout_authorize.showMenuByID = function (menuID) {
    $.get(page_content_path + '/api/system/authorize/user/permission/getMyMenuByID',{menuID:menuID},function(data){
        if (joyconn_layout.ValidataResult(data)) {
          var tree = data.data;
          if(tree){
            //   var root ={pId:menuID,pMenuID:menuID,pPid:0,pName:'菜单根节点',children:[]}
            //    joyconn_layout_authorize.convertToTree(root,tree);
            //    joyconn_layout_authorize.removeEmptyNode(root);
               joyconn_layout_authorize.showMenuData(tree);
          }
        }
      })

}


joyconn_layout_authorize.convertToTree=function(parent,list){
    if (!list){
        return []
    }
    list.sort(function(a,b){ 
        if (a.pPid!=b.pPid) {
            return a.pPid-b.pPid
        }else{
            return a.pSort-b.pSort
        }
    })
    $(list).each(function(i,obj){
        if(obj){
            if(obj.pPid==parent.pId){
                if(!parent.children){
                    parent.children=[]
                }
                parent.children.push(obj)
            }else{
                $(list).each(function(j,obj2){
                    if(obj2){
                        if(obj.pPid==obj2.pId){
                            if(!obj2.children){
                                obj2.children=[]
                            }
                            if($.inArray(obj,obj2.children)<0){
                                obj2.children.push(obj)
                            }                            
                        }
                    }
                })
            }
        }
    })

}
joyconn_layout_authorize.removeEmptyNode=function(list){
    if(list!=null&&list.length>0){
        var childModel;
        for(var i=0;i<list.length;){
            childModel= list[i];
            if(childModel.pType==2){
                 removeEmptyNode(childModel.children);
            }
            if(childModel.pType==3||(childModel.children!=null&&childModel.children.length>0)){
                i++;
            }else{
                delete childModel
            }
        }

    }

}
joyconn_layout_authorize.showMenuData=function(dataTree){
    var curPageUrl = window.location.pathname;

    function fillMenu(container, model) {
        var pUrl = model.pUrl;
        if(pUrl.indexOf('javascript:')!=0&&pUrl.indexOf('http://')!=0&&pUrl.indexOf('https://')!=0){
            pUrl =page_content_path+pUrl
        }
        var modelUrl =pUrl;
        if (modelUrl.indexOf(":") > 0) {
            modelUrl = modelUrl.substr(0, modelUrl.indexOf(":"));
        }
        if (modelUrl.pParams) {
            if (modelUrl.indexOf("?") > 0) {
                modelUrl += "&"+modelUrl.pParams;
            }else{
                modelUrl += "?"+modelUrl.pParams;
            }
        }

        if (model.pMenuID != 0) {
            var _element = $(' <li class="nav-item"><a class="nav-link" href="#"> <i class="nav-icon  ' + ((!model.pIcon || model.pIcon == "#") ? 'fa fa-circle-o' : model.pIcon) + '"></i><p> ' + model.pName + '</p></a></li>');
            if (model.children && model.children.length > 0) {
                _element.find('p:eq(0)').append(' <i class="fa fa-angle-left right"></i>')
                var ul = $('<ul class="nav nav-treeview"></ul>');
                $(_element).append(ul);
                $(model.children).each(function (index, _model) {
                    fillMenu(ul, _model);
                });
            } else {
                _element.find('a').attr('href', modelUrl)
            }
            $(container).append(_element);
            // if (model.pLevel == 1) {
               
            // } else if (model.pLevel == 2) {
            //     var li = $('<li  class="nav-item"><a  class="nav-link" href="' + modelUrl + ' "><i class="nav-icon  ' + ((!model.pIcon || model.pIcon == "#") ? 'far fa-circle' : model.pIcon) + '"></i>' + model.pName + '</a></li>');
            //     $(container).append(li);
            //     if (model.pUrl.toLowerCase() == curPageUrl.toLowerCase()) {
            //         // $(container).parent().addClass("active");
            //         // $(li).addClass("active");
            //     }
            // }
        }
    }

    // var curInMenu=false;
    if (dataTree.children && dataTree.children.length > 0) {

        $(dataTree.children).each(function (index, model) {
            fillMenu($("#MAIN_NAVIGATION_MENU"), model);
        });

        // var curPageUrl = window.location.pathname;
        // var active_ele = $(".menu_node").find("a[href='" + curPageUrl + "']");
        // $(active_ele).addClass("active");
        // $(active_ele).parents(".menu_node").addClass("active");
    }
    //  $("#MAIN_NAVIGATION_MENU").after(_html);
}


joyconn_layout_authorize.showUrl=function (appid,url,method) {
    return true;
}

